<?php
/**
 * 审批列表
 */
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class("Bll_Attendance_Examine");
apf_require_class('Tools_MultiPage');


class Oa_Attendance_ExamineController extends Oa_Attendance_AbstractController {

    const PAGEROWS=20;

    public function handle_request() {
        //只有hr有权限,查看这个页面
        $this->check_role();
        $params = $this->request->get_parameters();

        $examine_bll = new Bll_Attendance_Examine();
        $city_list = APF::get_instance()->get_config("oa_cities");

        $preurl="/examine/?";
        $preurl_ext=array();
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $where['apply_start'] = $params['start_time']." 00:00";
            $where['apply_end'] = $params['end_time']." 23:00";
            $preurl_ext[]= "start_time=".$params['start_time']."&end_time=".$params['end_time'];
        }
        //城市部门信息
        $def_depart=array();
        if(!empty($params['city'])){
            if(is_array($params['city'])){
                $where['citys'] = $params['city'];
                $params['city']=implode(',',$params['city']);
            }else{
                $where['citys'] = explode(',',$params['city']);
            }

            $preurl_ext[]= "city=".$params['city'];
            //默认选中城市的需要读取部门信息
            apf_require_class('Bll_Attendance_Department');
            $depart_bll = new Bll_Attendance_Department();
            $depart_list = $depart_bll->get_all_department();

            foreach($depart_list as $city=> $val){
                if(in_array($city,$where['citys'])){
                    foreach($val as $value){
                        $def_depart[]=$value;
                    }
                }
            }
            $this->request->set_attribute('def_depart',$def_depart);
        }
        if(!empty($params['keywords'])){
            $where['co_name'] = trim($params['keywords']);
            $preurl_ext[]= "&co_name=".$where['co_name'];
        }
        if(!empty($params['apart'])) {
            $where['apart'] = $params['apart'];
            $preurl_ext[]= "apart=".$where['apart'];
            $this->request->set_attribute('def_depart_name',$where['apart']);
        }

        $page = intval($params['p']);
        $page = $page<1?1:$page;
        $offset = ($page-1)*self::PAGEROWS;

        $datalist = $examine_bll->get_examine_list($where,self::PAGEROWS,$offset);
        $multipage='';
        $total_num = $datalist['count'];//总行数
        if($total_num){
            $url = "/examine/";
            $multipage = Tools_MultiPage::multiPage($url,$params, '', $total_num, self::PAGEROWS, 10);
        }
        /*$preurl .= implode("&",$preurl_ext);
        $pagelist['pre'] = $page ==1 ? '' : $preurl."&p=".($page-1);
        $pagelist['next'] = "";
        if($datalist['count'] > 20){
            $pagelist['next'] = $preurl."&p=".($page+1);
        }*/

        $this->request->set_attribute('city_list',$city_list);
        $this->request->set_attribute('check_city',$where['citys']);
        $this->request->set_attribute('page',$pagelist);
        $this->request->set_attribute('params',$params);
        $this->request->set_attribute('multipage',$multipage);
        $this->request->set_attribute('datalist',$datalist['datalist']);
        return 'Oa_Attendance_Examine';
    }
    //只有hr有权限
    private function check_role(){
        if($_SESSION['role_id']!=3){
            $this->response->redirect('/',false,false);return;
        }
    }

}